//
//  BankCardViewController.m
//  项目三
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "BankCardViewController.h"
#import "CollectView.h"
#import "MetalTableViewCell.h"
#import "bankCardView.h"
#import "ApplyView.h"

static NSString*const kUITableViewCellIndentifier = @"MetalCell";

@interface BankCardViewController () <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> {

    CollectView *_collectView;
    UIView *_backView;
    bankCardView *_detailView;
    ApplyView *_applyView;
}
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UITableView *tableView;

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation BankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}

- (void)initializeUserInterface {
    
    _backView = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 82, 708, 942))];
    _backView.backgroundColor = COLOR(205, 205, 205, 1);
    [self.view addSubview:_backView];
    
    self.titleImageView.image = [UIImage imageNamed:@"银行卡-logo"];
    [super initializeUserInterface];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"银行卡_选中"] forState:UIControlStateNormal];
    [btn setFrame:DHFlexibleFrame(CGRectMake(211, 24, 102, 32))];
    [btn addTarget:self action:@selector(respondsToSetBankBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIImageView *searchImageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(369, 16, 300, 32))];
    searchImageView.image = [UIImage imageNamed:@"搜索框"];
    [_backView addSubview:searchImageView];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setFrame:DHFlexibleFrame(CGRectMake(39, 13, 105, 36))];
    [saveBtn setBackgroundImage:[UIImage imageNamed:@"我的收藏"] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(respondsToSaveBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:saveBtn];
    
    [_backView addSubview:self.tableView];
    [_backView addSubview:self.searchField];
}

- (void)respondsToSaveBtnEvent:(UIButton *)sender {

    if (!_collectView) {
        _collectView = [[CollectView alloc] init];
    }
    if (![_backView.subviews containsObject:_collectView]) {
        [_backView addSubview:_collectView];
    }
}
- (void)respondsToSetBankBtn:(UIButton *)sender {

    [_applyView removeFromSuperview];
}
#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MetalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIndentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailImage.image = [UIImage imageNamed:@"申请"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!_detailView) {
        _detailView = [[bankCardView alloc] init];
    }
    if (![_backView.subviews containsObject:_detailView]) {
        [_backView addSubview:_detailView];
    }
    [_detailView.btn addTarget:self action:@selector(respondsToSetBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)respondsToSetBtn:(UIButton *)sender {

    [_detailView removeFromSuperview];
    _applyView = [[ApplyView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 82, 708, 922))];
    [self.view addSubview:_applyView];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    
}
#pragma mark - getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(41, 60, 627, 826)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 160;
        [_tableView registerNib:[UINib nibWithNibName:@"MetalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kUITableViewCellIndentifier];
        _tableView.layer.cornerRadius = 8;
        _tableView.layer.masksToBounds = YES;
    }
    return _tableView;
}
- (UITextField *)searchField {
    
    if (!_searchField) {
        _searchField = [[UITextField alloc] initWithFrame:DHFlexibleFrame(CGRectMake(375, 18, 260, 32))];
        _searchField.placeholder = @"请输入名称或者编号搜索";
        _searchField.textColor = [UIColor blackColor];
        _searchField.adjustsFontSizeToFitWidth = YES;
        _searchField.borderStyle = UITextBorderStyleNone;
        _searchField.delegate = self;
        _searchField.clearsOnBeginEditing = YES;
        _searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_searchField resignFirstResponder]; /**< 隐藏键盘 */
    }
    return _searchField;
}
@end
